package pers.gxm.composite;

public abstract class Entry {
    protected Entry parent;
//    个人答案
    protected String currentPath = "";

//    protected String absolutePath;
    public abstract String getName();
    public abstract int getSize();
    public Entry add(Entry entry) throws FileTreatmentException{
        throw new FileTreatmentException();
    }

    public void printList(){
        printList("");
    }

    protected abstract void printList(String prefix);

    public String getAbsolutePath(){
        StringBuilder sb = new StringBuilder();
        Entry entry = this;
        while(entry!= null){
            sb.insert(0,"/" + entry.getName());
            entry = entry.parent;
        }

        return sb.toString();
    }

    // 个人答案
    // 有缺陷，因为文件夹不一定是按照层级顺序依次创建，比如先创建子文件夹，再将子文件夹放入父文件夹中就会有问题。
    public String getAbsolutePath2(){
        return currentPath + "/" + getName();
    }

    public String toString (){
        return getName() + "(" + getSize() + ")";
    }


    public void setCurrentPath(String currentPath) {
        this.currentPath = currentPath;
    }
}
